import logging


class MyClient:
    def __init__(self, my_server, client):
        logging.info(f"MyClient.__init__")
        
        self.my_server = my_server
        self.client = client

        self.msg_handlers = {
            'login_request': self.on_login_request,

            'set_show_important': self.on_set_show_important,
            'set_show_normal': self.on_set_show_normal,
            'set_show_top': self.on_set_show_top,
            'set_show_author': self.on_set_show_author,
            
            'set_keywords': self.on_set_keywords,

            'open_article_page': self.on_open_article_page,
            'open_stock_page': self.on_open_stock_page,

            'buy_company_stock': self.on_buy_company_stock
        }

    def send_message(self, msg_type, msg_body):
        logging.info(f"MyClient.send_message, msg_type = {msg_type}, msg_body = {msg_body}")
        
        self.my_server.send_message(self.client, msg_type, msg_body)

    def on_message_received(self, msg_type, msg_body):
        logging.info(f"MyClient.on_message, msg_type = {msg_type}, msg_body = {msg_body}")

        msg_handler = self.msg_handlers.get(msg_type, None)
        if msg_handler is None:
            logging.warning(f"Failed to find message handler, msg_type = {msg_type}")
            return
                        
        msg_handler(msg_type, msg_body)        

    def on_login_request(self, msg_type, msg_body):
        """Login request handle function"""

        logging.info(f"MyClient.on_login_request, msg_type = {msg_type}, msg_body = {msg_body}")

        version = int(msg_body.get('version', 0))
        macaddr = msg_body.get('macaddr', '')

        msg_type = 'login_response'
        msg_body = {
            'success': True,
        }

        self.send_message(msg_type, msg_body)

    def on_set_show_important(self, msg_type, msg_body):
        """Send message back to client"""

        logging.info(f"MyClient.on_set_show_important, msg_type = {msg_type}, msg_body = {msg_body}")

        self.send_message(msg_type, msg_body)

    def on_set_show_normal(self, msg_type, msg_body):
        """Send message back to client"""
        
        logging.info(f"MyClient.on_set_show_normal, msg_type = {msg_type}, msg_body = {msg_body}")

        self.send_message(msg_type, msg_body)

    def on_set_show_top(self, msg_type, msg_body):
        """Send message back to client"""
        
        logging.info(f"MyClient.on_set_show_top, msg_type = {msg_type}, msg_body = {msg_body}")

        self.send_message(msg_type, msg_body)

    def on_set_show_author(self, msg_type, msg_body):
        """Send message back to client"""
        
        logging.info(f"MyClient.on_set_show_author, msg_type = {msg_type}, msg_body = {msg_body}")

        self.send_message(msg_type, msg_body)

    def on_set_keywords(self, msg_type, msg_body):
        """Send message back to client"""
        
        logging.info(f"MyClient.on_set_keywords, msg_type = {msg_type}, msg_body = {msg_body}")

        self.send_message(msg_type, msg_body)

    def on_open_article_page(self, msg_type, msg_body):
        """Send message back to client"""

        logging.info(f"MyClient.on_open_article_page, msg_type = {msg_type}, msg_body = {msg_body}")

        self.send_message(msg_type, msg_body)

    def on_open_stock_page(self, msg_type, msg_body):
        """Send message back to client"""

        logging.info(f"MyClient.on_open_stock_page, msg_type = {msg_type}, msg_body = {msg_body}")

        self.send_message(msg_type, msg_body)

    def on_buy_company_stock(self, msg_type, msg_body):
        """Send message back to client"""
        
        logging.info(f"MyClient.on_buy_company_stock, msg_type = {msg_type}, msg_body = {msg_body}")

        self.send_message(msg_type, msg_body)


